import {
  Router
} from 'express'
const router = Router()
import config from 'config'
import mysql from '../../libs/mysql'
import util from '../../util'
import marked from 'marked'
router.get('/',async (req,res,next)=>{
  let myConnect = await mysql.getConnect();
  let sql = `select a.id,a.title,a.updated_at,a.introduction, t.name as tag_name , c.name as category_name from articles as a left join tags as t on t.id = a.tag_id left join category as c on c.id = a.category_id`;
  let result = await mysql.query(myConnect,sql);
  await mysql.release(myConnect);
  res.render('articles', {
    'module_dir': config.admin.route,
    list:result
  })
})
router.get('/:id',async (req,res,next)=>{
  let id = req.params['id'];
  let myConnect = null;
  let defaultForm = {
    title:"",
    user_id:"",
    category_id:0,
    tag_id:0,
    content:"",
    read_num:0,
    content_html:"",
    introduction:"",
    articles_img:"",
    keyword:"",
    is_top:0
  }
  if(id){
    myConnect = await mysql.getConnect();
    let sql = `select title,user_id,category_id,tag_id,keyword,content,is_top,read_num,introduction,articles_img from articles where id = ${myConnect.escape(id)}`;
    let result = await mysql.query(myConnect,sql);
    if(result.length){
      result[0]['content_html'] = marked(result[0]['content'])
      defaultForm = Object.assign({},defaultForm,result[0])
    }
  }
  let tagssql = `select id,name from tags`;
  let categorySql = `select id,name from category`;
  let tags = await mysql.query(myConnect,tagssql);
  let categorys = await mysql.query(myConnect,categorySql);
  await mysql.release(myConnect);
  return res.render('articles_edit', {
    module_dir: config.admin.route,
    id,
    defaultForm,
    tags,
    categorys
  })
})
router.post('/edit',async (req,res,next)=>{
  let {pushForm,id} = req.body;
  let userid = config.user_id || req.session.user.id
  if(userid){
    let myConnect = await mysql.getConnect();
    let sql='';
    let params = null;
    let result = null;
    if(id==0){
      sql = `insert into articles(user_id,title,tag_id,category_id,content,introduction,articles_img,keyword,is_top) values (?)`;
      params = [userid,pushForm.title,pushForm.tag,pushForm.category,pushForm.content,pushForm.introduction,pushForm.articles_img,pushForm.keyword,pushForm.is_top];
      result = await mysql.query(myConnect,sql,params);
    }else{
      sql = `UPDATE articles SET title = ?, tag_id = ? ,category_id = ? ,content = ?,introduction = ?, articles_img = ? , keyword = ?,is_top = ? WHERE id = ${id}`;
      result = await mysql.query(myConnect,sql,pushForm.title,pushForm.tag,pushForm.category,pushForm.content,pushForm.introduction,pushForm.articles_img,pushForm.keyword,pushForm.is_top);
    }
    if(pushForm.category != pushForm.end_category_id){
      //不相等才进来(0-0)(1-1)
        //新记录+1
        await mysql.query(myConnect,`UPDATE category SET articles_num = articles_num + 1 WHERE id=${pushForm.category}`)
        //原记录-1
        await mysql.query(myConnect,`UPDATE category SET articles_num = articles_num - 1 WHERE id=${pushForm.end_category_id}`)
    }
    if(pushForm.tag != pushForm.end_tag_id){
        //新记录+1
        await mysql.query(myConnect,`UPDATE tags SET articles_num = articles_num + 1 WHERE id=${pushForm.tag}`)
        //原记录-1
        await mysql.query(myConnect,`UPDATE tags SET articles_num = articles_num - 1 WHERE id=${pushForm.end_tag_id}`)
    }
    console.log(result)
    await mysql.release(myConnect);
    if(result.affectedRows===1){
      return res.send(util.resSuccess({
        data:result
      }))
    }
    return res.send(util.resError({
      code:'user_id',
      msg:"插入错误"
    }))
  }else{
    return res.send(util.resError({
      code:'user_id',
      msg:"请登录"
    }))
  }
 
})
export default router;